package com.heima.admin.controller.v1;

import com.heima.admin.service.ChannelService;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.validator.ValidatorAdd;
import com.heima.model.common.validator.ValidatorUpdate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author mrchen
 * @date 2022/7/25 11:42
 */
@Api(value = "频道管理controller",tags = "频道管理controller")
@RestController  // @ResponseBody   @Controller
@RequestMapping("/api/v1/channel")
public class ChannelController {
    @Autowired
    private ChannelService channelService;

    @ApiOperation("根据id查询频道")
    @GetMapping("/one/{id}")
    public ResponseResult findOne(@NotNull @PathVariable Integer id) {
        return ResponseResult.okResult(channelService.getById(id));
    }

    @ApiOperation("查询全部频道")
    @GetMapping("/channels")
    public ResponseResult findAll() {
        List<AdChannel> list = channelService.list();
        return ResponseResult.okResult(list);
    }

    @ApiOperation(value = "根据条件分页查询",notes = " 根据条件  : name   status   进行查询，结果以ord升序排序")
    @PostMapping("/list")
    public ResponseResult findByQuery(@RequestBody ChannelDTO dto,@RequestHeader(value = "userId",required = false) String id){

        System.out.println("当前登陆用户id  {}" + id);

        return channelService.findByQuery(dto);
    }


    @ApiOperation(value = "保存频道")
    @PostMapping("/save")
    public ResponseResult save(@RequestBody @Validated(ValidatorAdd.class) AdChannel channel){
        return channelService.insert(channel);
    }
    @ApiOperation(value = "修改频道")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody @Validated(ValidatorUpdate.class) AdChannel channel){
        return channelService.update(channel);
    }

    @ApiOperation(value = "删除频道")
    @GetMapping("/del/{id}")
    public ResponseResult delete(@NotNull @PathVariable("id") Integer id){
        return channelService.deleteById(id);
    }
}
